package com.concise.appapi.controller;

import com.concise.appapi.controller.model.ReviewModel;
import com.concise.appapi.service.CommentService;
import com.concise.appapi.service.model.ArticleReviewListModel;
import com.initialize.pub.pojo.Result;
import com.initialize.pub.service.SignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jane
 * Date: 2020/2/28
 * Time: 6:06 下午
 * Description: 评论控制器
 * @author jane
 */

@Api(tags = "评论API")
@CrossOrigin
@RestController
@RequestMapping(value = "/comment")
public class CommentController {

    final
    SignService signService;

    final
    CommentService commentService;

    public CommentController(SignService signService, CommentService commentService) {
        this.signService = signService;
        this.commentService = commentService;
    }

    @ApiOperation(value = "给指定的文章评论")
    @PostMapping(value = "/post_comment/{sign}")
    public Result commentOnAGivenArticle(@RequestBody ReviewModel reviewModel, @PathVariable String sign) throws Exception {
        if (signService.judgingByObject(reviewModel, sign)) {
            boolean flag = commentService.commentOnAGivenArticle(reviewModel);
            if (flag) {
                return Result.success("提交成功");
            }
        } else {
            return Result.error("请勿重复请求");
        }
        return null;
    }

    @ApiOperation(value = "给指定的评论,评论")
    @PostMapping(value = "/user_comment/{sign}")
    public Result submitComment(@RequestBody Map<String,Object> reviewModel, @PathVariable String sign) throws Exception {
        if (signService.judgingByObject(reviewModel, sign)) {
            boolean flag = commentService.submitComment(reviewModel);
            if (flag) {
                return Result.success("提交成功");
            }
        } else {
            return Result.error("请勿重复请求");
        }
        return null;
    }

    @ApiOperation(value = "获取文章的所有评论")
    @GetMapping("all_reviews/{aid}")
    public Result getAllCommentsForAnArticle(@PathVariable String aid) {
        List<ArticleReviewListModel> allCommentsForAnArticle = commentService.getAllCommentsForAnArticle(aid);
        return Result.success(allCommentsForAnArticle);
    }

}
